﻿#coding: utf-8
import sys
import urllib
import os
import requests
import threading
from urllib import request
import json
import socket
import time

TIME = 20                      # 单位是s
socket.setdefaulttimeout(TIME) # 设置延迟时间，请求超出时间就会断开

class MyThread(threading.Thread):
    """
    属性:
    target: 传入外部函数, 用户线程调用
    args: 函数参数
    """
    def __init__(self, target, args):
        print("a new thread")
        super(MyThread, self).__init__()  #调用父类的构造函数 
        self.target = target
        self.args = args

    def run(self) :
        self.target(self.args)

# list&detail
def get_html(url_address):
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
    req = urllib.request.Request(url=url_address, headers=headers, method = 'POST')
    calc = 0
    while(True):
        try:    
            ht = urllib.request.urlopen(req)
            break;
        except :
            print("获取网页超时！！！",calc);
            calc += 1
            if(calc == 20):
                ht = None
                print("网页获取失败！！！");
                break;
            time.sleep(20)
    return ht

def get_json(ht):
    bs = ht.read()
    #print(bs)
    j = json.loads(bs.decode('utf-8'))
    return j

def get_list(p):
    url = "http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsList&on=true&pageSize=15&conditionType=1&page="+str(p)
    #print("list=> "+url)
    try:
        jsonht = get_html(url)
        j = get_json(jsonht)
        #print(json["pageCount"])
        datalist = j["list"]
        #ids=[]
        for each in datalist:
            #ids.append(each["ID"])
            get_detail(each["ID"])
    except: 
        print(url)

def get_detail(id):
    url = "http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsById&id="+id
    #print("detail=> "+url)
    jsonht = get_html(url)
    j = get_json(jsonht)
    print(j["businessLicenseNumber"],j["businessPerson"],j["certStr"],j["epsAddress"],j["epsName"],j["epsProductAddress"],j["legalPerson"],j["productSn"],j["qfManagerName"],j["qualityPerson"],j["rcManagerDepartName"],j["rcManagerUser"],j["xkDate"],j["xkDateStr"],j["xkName"],j["xkType"])


if __name__ == '__main__':
    #parser = str(int(sys.argv[1]))      # 312
    #print("starting "+parser)

    for i in range(200,312):
        get_list(str(i))

    #print("ended " + parser)




